﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace FmcWebsite.Core
{
    public interface IDbConnect
    {        
        DataTable Select(string cmd);
        DataTable Select(string cmd, IEnumerable<DbParameter> parameters);
        bool ExecDDL(string cmd);
        bool ExecDDL(string cmd, IEnumerable<DbParameter> parameters);
        DbParameter CreateParameter(string name, object value);
        void Close();
    }

    /// <summary>
    /// You can extend this class to put static factory
    /// to create a specific IDBConnect object.
    /// </summary>
    public partial class DBConnect
    {
        static public IDbConnect Database = null;

        /// <summary>
        /// Try to get an ID-value from the row/field.        
        /// </summary>
        /// <param name="row">data row of the field</param>        
        /// <param name="columnname">field name</param>
        /// <returns>If field is empty then -1 will be returned, otherwise the 
        /// value of the field</returns>
        static public int TryGetId(DataRow row, string columnname)
        {
            try
            {
                return row.Field<int>(columnname);
            }
            catch
            {
                return -1;
            }
        }
    }

}
